﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Diagnostics;

namespace SoftwareLogistics.SharepointClient.Library.Models
{
    public class SPWhereClause
    {
        public enum ComparisonTypes
        {
            LessThan,
            Equal,
            NotEqual,
            GreaterThan,
            In,
            IsNotNull,
            IsNull
        }        

        public ComparisonTypes ComparisonType { get; set; }

        public string Field { get; set; }
        public Object Value { get; set; }
        public List<Object> Values { get; set; }        

        public static SPWhereClause Create(ComparisonTypes type, string fieldName, object value)
        {
            var clause = new SPWhereClause();
            clause.ComparisonType = type;
            clause.Field = fieldName;
            clause.Value = value;

            return clause;
        }

        public static SPWhereClause CreateIn(string fieldName, params object[] values)
        {
            var clause = new SPWhereClause();
            clause.ComparisonType = ComparisonTypes.In;
            clause.Field = fieldName;
            clause.Values = new List<object>();
            foreach(var value in values)
                clause.Values.Add(value);
            
            return clause;
        }

        private string GetValueString()
        {
            if (Value == null)
                return string.Empty;

            if (Value.GetType() == typeof(DateTime))
                return Common.GetSPDateFormat((DateTime)Value);
            else
                return Value.ToString();
        }

        public XElement GetXML()
        {
            var name = string.Empty;
            switch (ComparisonType)
            {
                case ComparisonTypes.Equal: name = "Eq"; break;
                case ComparisonTypes.GreaterThan: name = "Gt"; break;
                case ComparisonTypes.LessThan: name = "Lt"; break;
                case ComparisonTypes.NotEqual: name = "Neq"; break;
                case ComparisonTypes.In: name = "In"; break;
                case ComparisonTypes.IsNotNull: name = "IsNotNull"; break;
                case ComparisonTypes.IsNull: name = "IsNull"; break;
            }

            var element = new XElement(name);
            var fieldElement = new XElement("FieldRef",new XAttribute("Name",Field));

            element.Add(fieldElement);
            if (ComparisonType == ComparisonTypes.In)
            {
                var valuesElement = new XElement("Values");

                foreach (var value in Values)
                {
                    var valueElement = new XElement("Value");
                    valueElement.Value = GetValueString();
                    valuesElement.Add(valueElement);
                }

                element.Add(valuesElement);
            }
            else
            {
                var valueElement = new XElement("Value");
                XAttribute typeAttribute = null;
                Debug.WriteLine(Value.GetType().ToString());

                switch(Value.GetType().ToString())
                {
                    case "System.Int32": typeAttribute = new XAttribute("Type","Integer"); break;
                    case "System.DateTime": typeAttribute = new XAttribute("Type", "DateTime"); break;
                    case "System.String": typeAttribute = new XAttribute("Type", "Text"); break;
                    case "SoftwareLogistics.SharepointClient.Library.Models.SPUser": typeAttribute = new XAttribute("Type", "Integer"); break;
                }

                if (typeAttribute != null)
                    valueElement.Add(typeAttribute);

                if (Value.GetType().ToString() == "SoftwareLogistics.SharepointClient.Library.Models.SPUser")
                {
                    var userIdElement = new XElement("UserID");
                    userIdElement.Value = ((SPUser)Value).ID.ToString();
                    valueElement.Add(userIdElement);
                }
                else
                {

                    valueElement.Value = GetValueString();
                }
                element.Add(valueElement);
            }

            return element;
        }


    }
}
